package spindles.gwt.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import spindles.api.domain.Settings;
import spindles.api.domain.ThresholdGroup;
import spindles.api.util.UserException;
import spindles.gwt.shared.GWTException;
import spindles.gwt.shared.SettingsDTO;
import spindles.gwt.shared.SettingsManager;

public class SettingsServletStub extends StubServlet implements SettingsManager{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2255809590995626364L;

	@SuppressWarnings("unchecked")
	public SettingsDTO getSettings() {				
		
		Settings s = new Settings();
		s.setDefaultGroupName(ThresholdGroup.ThresholdGroupName.GROUP_9_3.name());
		s.setExportDirectory("C:\\export");
		s.setShowOnlyRawData(false);
		
		SettingsDTO dto = (SettingsDTO)ServletUtil.map(s, SettingsDTO.class);
		
		List<ThresholdGroup> threshholdGroups = ThresholdGroup.findAll();
		Map groups = new HashMap();		
		for(ThresholdGroup group : threshholdGroups){
			groups.put(group.getGroupName(), group.toString());
		}
		
		dto.setThresholdGroups(groups);
		
		return dto;
	}

	public void saveSettings(SettingsDTO dto) throws GWTException {		
		try {
			ServletUtil.map(dto, Settings.class);
			//does nothing else
		} catch (Exception e) {
			throw new GWTException(e);
		}		
	}
	
	public void detectSpindleIndications(String groupName) throws GWTException {
		try {
			if (ServletUtil.isImporting()) {
				throw new UserException("Cannot run this operation while importing data.");
			}
			ServletUtil.setImportStatus(true);
			
			//run the lengthy operation
			Thread.sleep(30000);
		} catch (Exception e) {
			throw new GWTException(e);
		} finally {
			ServletUtil.setImportStatus(false);
		}
		
	}
	
	

}
